/* 
 * Copyright (c) 2019, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.portal;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.beust.jcommander.internal.Lists;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.admin.sys.domain.AdminPrincipal;
import com.cory.admin.sys.domain.entity.SysResource;
import com.cory.admin.sys.domain.portal.FolderVO;
import com.cory.admin.sys.domain.portal.MenuVO;
import com.cory.admin.sys.domain.portal.ServiceVO;
import com.cory.admin.sys.enums.EResourceType;
import com.cory.boot.base.BaseController;
import com.cory.boot.domain.bean.Result;
import com.google.common.collect.ImmutableMap;

/**
 * 服务
 * @author xiongcong
 * @date 2019-02-27
 */
@Controller
@RequestMapping("portal/service")
public class ServiceController extends BaseController {
    
    @GetMapping("")
    public String index(Integer serviceId) {
        assertNotNull(serviceId);
        return String.format("portal/%s/service", AdminConstants.MODEL);
    }
    
    @RequestMapping("getMenuList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Result<?> getMenuList(HttpSession session, Integer serviceId) {
        assertNotNull(serviceId);
        AdminPrincipal user = (AdminPrincipal)session.getAttribute(AdminConstants.SESSION_USER_INFO);
        List<SysResource> resources = (List<SysResource>)session.getAttribute(AdminConstants.SESSION_USER_RESOURCE);
        
        List<SysResource> services = resources.stream().filter(i -> i.getType() == EResourceType.SERVICE.key).collect(Collectors.toList());
        SysResource service = services.stream().filter(i -> i.getId().equals(serviceId)).findFirst().get();
        
        ServiceVO serviceVO = new ServiceVO(service.getId(), service.getName(), service.getServiceType(), service.getIcon());
        
        List<FolderVO> folderVOs = resources.stream().filter(folder -> folder.getPid().equals(service.getId()))
                .map(folder -> new FolderVO(folder.getId(), folder.getName(), folder.getIcon())).collect(Collectors.toList());
        
        folderVOs.forEach(folder -> {
            List<SysResource> menus = resources.stream().filter(menu -> menu.getPid().equals(folder.getId())).collect(Collectors.toList());
            List<MenuVO> menuVOs = menus.stream().map(i -> new MenuVO(i.getId(), i.getName(), i.getCode()).msgCodes(i.getMsgCode(), user.getTenantId())).collect(Collectors.toList());
            folder.setMeuns(menuVOs);
        });
        
        //return Result.success("ok", ImmutableMap.of("services", serviceVOs, "menus", folderVOs));
        return Result.success("ok", ImmutableMap.of("service", serviceVO, "menus", folderVOs));
    }
    
    @RequestMapping("getAllMenuList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Result<?> getAllMenuList(HttpSession session, Integer serviceId) {
        AdminPrincipal user = (AdminPrincipal)session.getAttribute(AdminConstants.SESSION_USER_INFO);
        List<SysResource> resources = (List<SysResource>)session.getAttribute(AdminConstants.SESSION_USER_RESOURCE);
        
        List<SysResource> services = resources.stream().filter(i -> i.getType() == EResourceType.SERVICE.key).collect(Collectors.toList());
        
        List<Map<String, Object>> list = Lists.newArrayList();
        for (SysResource service : services) {
            ServiceVO serviceVO = new ServiceVO(service.getId(), service.getName(), service.getServiceType(), service.getIcon());
            
            List<FolderVO> folderVOs = resources.stream().filter(folder -> folder.getPid().equals(service.getId()))
                    .map(folder -> new FolderVO(folder.getId(), folder.getName(), folder.getIcon())).collect(Collectors.toList());
            
            folderVOs.forEach(folder -> {
                List<SysResource> menus = resources.stream().filter(menu -> menu.getPid().equals(folder.getId())).collect(Collectors.toList());
                List<MenuVO> menuVOs = menus.stream().map(i -> new MenuVO(i.getId(), i.getName(), i.getCode()).msgCodes(i.getMsgCode(), user.getTenantId())).collect(Collectors.toList());
                
                folder.setMeuns(menuVOs);
            });
            
            list.add(ImmutableMap.of("service", serviceVO, "menus", folderVOs));
        }
        
        //return Result.success("ok", ImmutableMap.of("services", serviceVOs, "menus", folderVOs));
        return Result.success("ok", list);
    }
}
